﻿// -----------------------------------------------------------------------
// <copyright file="HttpWebHelper.cs" company="TaskyMedia LLC">
//  Copyright (c) TaskyMedia LLC
// </copyright>
// -----------------------------------------------------------------------

namespace SohpClient
{
    using System;
    using System.Globalization;
    using System.Net;
#if SILVERLIGHT
    using System.Net.Browser;
#endif

    /// <summary>
    /// Contains methods to assist in the creation of <see cref="HttpWebRequest"/> objects.
    /// </summary>
    internal static class HttpWebHelper
    {
        /// <summary>
        /// Gets a <see cref="HttpWebHelper"/> instance for the <c>open</c> operation.
        /// </summary>
        /// <param name="hostBase">The SOHP host base URI.</param>
        /// <param name="targetHost">The target host.</param>
        /// <param name="targetPort">The target port.</param>
        /// <param name="protocolVersion">The SOHP protocol version.</param>
        /// <returns>Returns a <see cref="HttpWebHelper"/> object to be used in the request.</returns>
        internal static HttpWebRequest GetOpenRequest(string hostBase, string targetHost, int targetPort, string protocolVersion)
        {
            string reqUrl = FormatUrl(hostBase, "open") + string.Format(CultureInfo.InvariantCulture, "?hostName={0}&port={1}", targetHost, targetPort);
            return CreateRequest(reqUrl, protocolVersion);
        }

        /// <summary>
        /// Gets a <see cref="HttpWebHelper"/> instance for the <c>send</c> operation.
        /// </summary>
        /// <param name="hostBase">The SOHP host base URI.</param>
        /// <param name="protocolVersion">The SOHP protocol version.</param>
        /// <returns>
        /// Returns a <see cref="HttpWebHelper"/> object to be used in the request.
        /// </returns>
        internal static HttpWebRequest GetSendRequest(string hostBase, string protocolVersion)
        {
            string reqUrl = FormatUrl(hostBase, "send");
            return CreateRequest(reqUrl, protocolVersion);
        }

        /// <summary>
        /// Gets a <see cref="HttpWebHelper"/> instance for the <c>receive</c> operation.
        /// </summary>
        /// <param name="hostBase">The SOHP host base URI.</param>
        /// <param name="protocolVersion">The SOHP protocol version.</param>
        /// <returns>
        /// Returns a <see cref="HttpWebHelper"/> object to be used in the request.
        /// </returns>
        internal static HttpWebRequest GetReceiveRequest(string hostBase, string protocolVersion)
        {
            string reqUrl = FormatUrl(hostBase, "receive");
            return CreateRequest(reqUrl, protocolVersion);
        }

        /// <summary>
        /// Creates a <see cref="HttpWebRequest"/> for the specified URI.
        /// </summary>
        /// <param name="uri">The URI.</param>
        /// <param name="protocolVersion">The protocol version.</param>
        /// <returns>Returns a new <see cref="HttpWebRequest"/> object.</returns>
        internal static HttpWebRequest CreateRequest(string uri, string protocolVersion)
        {
#if SILVERLIGHT
            WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
            WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);
#endif 
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
            req.Headers["X-SOHP-ClientVersion"] = protocolVersion;

            return req;
        }

        /// <summary>
        /// Formats a URL to properly concatenate the host base URI and operation name.
        /// </summary>
        /// <param name="hostBase">The SOHP host base URI.</param>
        /// <param name="operationName">Name of the operation.</param>
        /// <returns>Returns a property formatted and concatenated URI string.</returns>
        private static string FormatUrl(string hostBase, string operationName)
        {
            if (hostBase.EndsWith("/", StringComparison.OrdinalIgnoreCase))
            {
                return string.Format(CultureInfo.InvariantCulture, "{0}{1}", hostBase, operationName);
            }
            else
            {
                return string.Format(CultureInfo.InvariantCulture, "{0}/{1}", hostBase, operationName);
            }
        }
    }
}